/*
 * @Description:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 14:48:02
 */
import { FeatureManager } from '@amcax/base';
import { CmdDivider } from '@app-cad/components/CommandBar';
import { cmdNewMark } from '@app-base/common/commands/cmd-new-mark';
import {
  cmdPickFace,
  cmdPickLine,
  cmdPickModel,
  cmdPickPoint,
  cmdPickSmart,
} from '@app-base/common/commands/cmd-pickType';
import { cmdFitView } from '@app-cad-env/nextcad/commands/feature/cmd-fit-view';
import { cmdCheckTopology } from '@app-cad/common/commands/cmd-check-topology';
import {
  cmdRenderAllEdge,
  cmdRenderHiddenEdge,
  cmdRenderNoEdge,
  cmdRenderPhysicalMaterial,
  cmdRenderWireframe,
} from '@app-cad/common/commands/cmd-renderMode';
import {
  cmdPanView,
  cmdRotateView,
  cmdZoomView,
  KEYBOARD_VIEW,
} from '@app-cad/common/commands/cmd-toolbar-mouse-control';
import {
  cmdBackView,
  cmdBottomView,
  cmdFrontView,
  cmdIsometricView,
  cmdLeftView,
  cmdRightView,
  cmdTopView,
  cmdVerticalView,
} from '@app-cad/common/commands/cmd-view-classification';
import { CONSTRUCTION_LINE } from '@app-cad/common/utils';
import { userEvents } from '@app-cad/module';
import React, { useEffect } from 'react';
import {
  SplitToolButton,
  ToggleToolButton,
  ToolButton,
} from '../../../../common/components/toolbar-components';
import { cmdConstLine } from '../../commands/sketch/cmd-const-line';
import { sketchEventCenter } from '../../events/SketchEventCenter';
import { cmdResizeToArea } from '@app-base/common/commands/cmd-resize-to-area';

type Params = {
  flag?: boolean;
  type: 'isShow' | 'isSelect';
  isActive?: boolean;
};

export function ToolBar() {
  const [consLineShow, setConsLineShow] = React.useState(false);
  const [consIsActive, setConsIsActive] = React.useState(false);
  const [isViewControlActive, setIsViewControlActive] = React.useState(false);

  useEffect(() => {
    registerEvents();
    return () => {
      unregisterEvents();
    };
  }, []);

  useEffect(() => {
    setConsIsActive(sketchEventCenter().getLineType() === 1);
  }, [consLineShow]);

  const registerEvents = () => {
    userEvents.on(KEYBOARD_VIEW, ({ isSelected }) => {
      if (isSelected) {
        setIsViewControlActive(true);
      } else {
        setIsViewControlActive(false);
      }
    });
    userEvents.on(
      CONSTRUCTION_LINE,
      ({ flag: isExist, type = 'isShow' }: Params) => {
        if (type === 'isShow') {
          setConsLineShow(isExist);
          setConsIsActive(false);
        } else if (type === 'isSelect') {
          const curve = sketchEventCenter()
            .getSelectedModels()
            .find((item) => item.type !== 'Point');
          if (curve) {
            setConsIsActive(curve.curveStyle !== 0);
          } else {
            setConsIsActive(sketchEventCenter().getLineType() !== 0);
          }
        }
      },
    );
  };
  const unregisterEvents = () => {};

  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'row',
        gap: 8,
        padding: 4,
      }}>
      <ToggleToolButton
        activatedCmdIndex={0}
        cmds={[
          cmdPickSmart,
          cmdPickModel,
          cmdPickFace,
          cmdPickLine,
          cmdPickPoint,
        ]}
      />
      {FeatureManager.isFeatureEnabled('toolBar-view') ? (
        <SplitToolButton
          activated={isViewControlActive}
          cmds={[cmdPanView, cmdRotateView, cmdZoomView]}
        />
      ) : null}
      <CmdDivider />

      {FeatureManager.isFeatureEnabled('view-classification') ? (
        <SplitToolButton
          cmds={[
            cmdFrontView,
            cmdBackView,
            cmdLeftView,
            cmdRightView,
            cmdTopView,
            cmdBottomView,
            cmdIsometricView,
            cmdVerticalView,
          ]}
        />
      ) : null}
      {FeatureManager.isFeatureEnabled('render-mode') ? (
        <SplitToolButton
          cmds={[
            cmdRenderAllEdge,
            cmdRenderHiddenEdge,
            cmdRenderNoEdge,
            cmdRenderWireframe,
            cmdRenderPhysicalMaterial,
          ]}
        />
      ) : null}
      <ToolButton cmd={cmdFitView} />
      <ToggleToolButton cmds={[cmdCheckTopology]} />
      {FeatureManager.isFeatureEnabled('electron-show-port') && (
        <ToolButton cmd={cmdNewMark} />
      )}
      {consLineShow && (
        <ToggleToolButton
          isActivated={consIsActive}
          cmds={[cmdConstLine]}
        />
      )}
      <ToggleToolButton cmds={[cmdResizeToArea]} />
    </div>
  );
}
